<?
session_start();
if (!isSet($_SESSION['uid'])) {
    print "<meta http-equiv=\"refresh\" content=\"0;url=index.html\">";
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<link href='http://fonts.googleapis.com/css?family=Donegal+One' rel='stylesheet' type='text/css'>
		<title>Dashboard</title>
		<meta name="viewport" content="width=1280, user-scalable=no"/>  
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="style.css" />
        <link rel="stylesheet" href="dashboard.css" />
        <script src="./jquery.min.js" type="text/javascript"></script>	
		<script type="text/javascript">
		function before_submit(){
			document.getElementById("ex_id").value=event.target.id;
		}
		function goto_exercise(exercise_id) {
			var f = $('<form action="exercise.php" method="post">');
			f.html('<input type="hidden" name="exercise_id" value="'+exercise_id+'" />');
			f.submit();
		}
		</script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#tab-exercise").click(function(){
					$("#exercise-list").show();
                    $("#tab-exercise").css('border-right', '0px');
                    $("#tab-exercise").css('background-image', 'url(paper.jpg)');
					$("#game-list").hide();
                    $("#tab-game").css('border-right', '1px solid #333');
                    $("#tab-game").css('background-image', 'none');
				});
				$("#tab-game").click(function(){
					$("#game-list").show();
                    $("#tab-game").css('border-right', '0px');
                    $("#tab-game").css('background-image', 'url(paper.jpg)');
					$("#exercise-list").hide();
                    $("#tab-exercise").css('border-right', '1px solid #333');
                    $("#tab-exercise").css('background-image', 'none');
                });
                var content = $('#content-panel');
                var side = $('#left-panel');
                if (content.height() > side.height()) {
                        side.height(content.height());
                } else {
                        content.height(side.height());
                }
			});
		</script>
	</head>
	
	<body>
    <div id="wrapper">
		<div id="left-panel">
			<div id="personal-infos">
                <h2><? print $_SESSION['nick_name'];  ?></h2>
<?
//conect to mysql
$db = mysql_connect("localhost", "usr_2012_16", "se09member");
if (!$db) {
	print "Error - Could not connect to MySQL";
}
// Select the database
$er = mysql_select_db("db_2012_16");
if (!$er) {
	print "Error - Could not select the database db_2012_16";
}
$query = "select score from se_user where uid = " . $_SESSION['uid'];
// Execute the query
$result = mysql_query($query);
if (!$result) {
	print "Error - the query could not be executed";
	$error = mysql_error();
	print "<p>" . $error . "</p>";
}
else if (mysql_num_rows($result) == 0){
	print "<p>no such user or Password error, system will return automatically in 3 seconds...</p>";
	print "<meta http-equiv=\"refresh\" content=\"3;url=index.html\">";
}
$row = mysql_fetch_array($result);
?>
                <p>积分: <? print $row['score']; ?></p>
                <a href="logout.php"><div class="fake_link">注销</div></a>
            </div>
			<div id="tab-exercise" class="list-item clickable">教学练习</div>
			<div id="tab-game" class="list-item clickable">游戏</div>
			<div id="top-chart">
                <h2>排行榜</h2>
<?
$query = "select nick_name, score from se_user order by score desc;";
$result = mysql_query($query);
if ($result) {
    $row_cnt = mysql_num_rows($result);
    if ($row_cnt > 10) $row_cnt = 10;
    for ($i = 0; $i < $row_cnt; $i++) {
        $row = mysql_fetch_array($result);
?>
                <div class="table_row">
                    <div class="table_cell"><? print $row['nick_name']; ?></div>
                    <div class="table_cell"><? print $row['score']; ?></div>
                </div>
<?
    }
}
?>
            </div>
		</div>
		
		<div id="content-panel">
			<div id="exercise-list">
				<div class="list-item composite-full-width">
					<div class="title-col"><h3>标题</h3></div>
					<div class="description-col"><h3>描述</h3></div>
				</div>
				<a href="lesson1.html">
                <div class="list-item composite-full-width clickable">
						<div class="title-col">声母教学</div>
						<div class="description-col">第一课声母教学，教授zh、ch、sh的打法，简单的练习。</div>
					</div>
				</a>
<?
$query = "select * from se_exercise_meta;";
$result = mysql_query($query);
if (!$result) {
	print "Error - the query could not be executed";
	$error = mysql_error();
	print "<p>" . $error . "</p>";
}
//else if (mysql_num_rows($result) == 0){
//	print "<p>Sorry, there are 0 exercises in our database...</p>";
//}
else{
	while($row = mysql_fetch_array($result)){?>
				<div class="list-item composite-full-width clickable" onclick="goto_exercise(<? print $row['id']?>)">
					<div class="title-col"><? print $row['title'] ?></div>
					<div class="description-col"><? print $row['description'] ?></div>
				</div>
<?
	}
}
?>										
			</div>
			<div id="game-list">
                <div class="list-item composite-full-width">
                    <div class="title-col"><h3>标题</h3></div>
                    <div class="description-col"><h3>描述</h3></div>
                    </div>
                    <a href="game1.php">
                        <div class="list-item composite-full-width clickable">
                            <div class="title-col">小游戏</div>
                            <div class="description-col">嘿我一是只老掉牙的小游戏，进来试试看啵>.<！！！</div>
                        </div>
                    </a>
            </div>
		</div>
    </div>
	</body>
</html>

